wordpress调用当前分类下子分类的方法
这样我们通过php的foreach循环用很少的代码就将一个分类下的文章数目统计出来了。
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)}
{
<?php
{
{
while($this_category->category_parent) // 若当前分类有上级分类时,循环
}
{
{
1.现在function.php里面添加下面的代码:
$sub_parent_array = get_categories('parent='.$v->cat_ID);获得WordPress指定分类(包括子分类)下的所有文章数,代码如下:
}echo '<ul>';
}
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
{
//将第一级和第二级和第三级统计的文章数目进行相加后放到一个变量中。
$total = $count+$sub_count+$three_count;
if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
实例2:
}$parent_array = get_categories('hide_empty=0&parent=79');
?>
在企业网站中,点击根分类时,显示当前根分类下的子分类,这是个很常见的需求,大多cms也能实现这个功能,如果使用wordpress架构,可以吗?
return $this_category->term_id; // 返回根分类的id号echo '</ul>';
return $this_category->term_id; // 返回根分类的id号
{
while($this_category->category_parent) // 若当前分类有上级分类时,循环
function get_category_root_id($cat)
foreach($sub_parent_array as $kk=>$vv) //第二步
接着就是要获取当前分类的父ID,这个也是本文的重中之重,扒了很多资料,也没找到直接可以实现的,不过通过一个函数,倒可以间接获取,代码如下:
$three_parent_array = get_categories('hide_empty=0&parent='.$vv->cat_ID);}
echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
{
{
$count +=$v->category_count; //第一级子分类下文章数进行统计
//parent 父级分类的ID号
foreach($parent_array as $k=>$v) //第一步
希望本文所述对大家的WordPress建站有所帮助。
其实主要用到wp_list_categorys()函数,该函数的child_of参数是一个数字,显示指定ID(也就是所填的这个数字)下的子分类,这样只要找到当前分类根分类的ID就可以显示了。
the_category_ID()用于显示当前页面的分类ID,默认是输出的,作为参数传递时,最好传入一个false参数,即the_category_ID(false)获取当前分类ID。
$this_category = get_category($cat); // 取得当前分类2.然后在页面要显示二级分类的地方粘贴下面这段代码即可
自己没用过wordpress博客但是个人认为wordpress有函数可直接来子调用当前分类下的子分类的,但是我找了很久没找到,后来找到一具朋友自己的做法,下面我来整理一下.

答案是肯定的,wordpress也可以实现这样的功能.
foreach($three_parent_array as $kkk=>$vvv) //第三步现在就万事具备了,我们就实现一下吧,代码如下:
}$sub_count +=$vv->category_count; //第二级子分类下文章数进行统计
//使用get_categories()函数,里面参数的意思是hide_empty把子分类下没有文章的也显示出来
function get_category_root_id($cat)
$three_count +=$vvv->category_count; //第三极子分类下文章数进行统计
wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=");
}
if(is_single()||is_category())
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jz/wp/4864.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
ludou_se_only); } add_action(s
时间:2021-01-23
-
所以经常导致输错密码的
时间:2021-01-23
-
WordPress措施打开速度慢的
时间:2021-01-22
-
WordPress回覆评论自动添加
时间:2021-01-22
-
我们可以通过下面的代码
时间:2021-01-22
-
如何修改WordPress默认脚色
时间:2021-01-20
-
同样的修改文件就行
时间:2021-01-20
-
少数善意的转载者既使没
时间:2021-01-20
热门文章
-
WordPress新手安装教程(图文)
时间:2020-12-28
-
在设置菜单中增加WordPress私密 隐藏的全部
时间:2021-01-10
-
WordPress博客程序常见错误的解决方法
时间:2020-12-28
-
WordPress程序打开速度慢的三种解决方法
时间:2021-01-10
-
wordpress使用代码在每篇文章尾部添加版权
时间:2020-12-14
-
WordPress博客措施常见错误的办理要领
时间:2021-01-15
-
关于WordPress的SEO优化相关的一些PHP页面脚
时间:2021-01-07
-
WordPress措施打开速度慢的三种办理要领
时间:2021-01-22
-
详解WordPress中的头像缓存和署理中的缓存
时间:2021-01-15
-
少数善意的转载者既使没有看到版权信息
时间:2021-01-20
